﻿package com.study.zouchao.superproject.login.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.ScrollView;

import com.study.zouchao.superproject.R;

import com.study.zouchao.superproject.base_zou.MyBaseFragment;
import com.study.zouchao.superproject.base_zou.ZouImgLoader;
import com.study.zouchao.superproject.login.contract.ILoginContract;
import com.study.zouchao.superproject.login.presenter.LoginPresenter;
import com.study.zouchao.superproject.logininterceptor.LoginCarrier;
import com.study.zouchao.superproject.logininterceptor.LoginInterceptor;
import com.study.zouchao.superproject.logininterceptor.LoginStatusUtil;
import com.study.zouchao.superproject.main.view.MainActivity;
import com.study.zouchao.superproject.register.RegisterActivity;
import com.study.zouchao.superproject.util.EventBusEvent;
import com.study.zouchao.superproject.util.EventBusEvent_C;
import com.study.zouchao.superproject.util.EventBusUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/1/20.
 */

public class LoginFragment extends MyBaseFragment implements ILoginContract.ILoginView {
    private static final String TAG = "LoginF";
    //P
    private ILoginContract.ILoginPresenter mPresenter;

    @BindView(R.id.id_edit_username)
    AutoCompleteTextView mEditUser;

    @BindView(R.id.id_edit_password)
    EditText mEditPwd;

    @BindView(R.id.id_login_form)
    ScrollView mLoginForm;

    @BindView(R.id.id_login_progress)
    ProgressBar mLoginProgress;

    @BindView(R.id.id_iv_progress_loading)
    ImageView mIvLoading;



    /**
     * 监听器
     */
    @OnClick({R.id.id_btn_login, R.id.id_btn_clear, R.id.id_btn_islogin, R.id.id_tv_register})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_btn_login :
                //登陆
                attemptLogin();
                break;
            case R.id.id_btn_clear :
                mPresenter.onDestoryPresnter();
                break;
            case R.id.id_btn_islogin :
                Log.i("登陆状态..", LoginStatusUtil.getLoginStatus(getContext()) +"\n"+ LoginStatusUtil.getLoginData(getContext()));
                break;
            case R.id.id_tv_register :
                RegisterActivity.actionStartRegisterActivity(getActivity());
                break;
        }
    }

    /**
     * 尝试登陆
     */
    private void attemptLogin() {
        //得到登陆的账号密码
        String user = mEditUser.getText().toString().trim();
        String pwd = mEditPwd.getText().toString().trim();
        mPresenter.attemptLogin(user, pwd);
    }

    /*
     *  View
     *
     */
    @Override
    public void showLogining(boolean isLogining) {
        //登陆form显示与隐藏
        mLoginForm.setVisibility(isLogining ? View.GONE : View.VISIBLE);
        //登陆Progress显示与隐藏
        mLoginProgress.setVisibility(isLogining ? View.VISIBLE : View.GONE);

//        mIvLoading.setVisibility(isLogining ? View.VISIBLE : View.GONE);
        //
//        ZouImgLoader.loadImage(getActivity(), mIvLoading, R.drawable.loading_pink, R.drawable.error);
        //
    }

    @Override
    public void setUsernameError(String error) {
        //显示错误信息
        mEditUser.setError(error);
        //获取焦点
        if ( ! TextUtils.isEmpty(error)) {
            mEditUser.requestFocus();
        }
    }

    @Override
    public void setPwdError(String error) {
        //显示错误信息
        mEditPwd.setError(error);
        //获取焦点
        if( ! TextUtils.isEmpty(error)) {
            mEditPwd.requestFocus();
        }
    }

    @Override
    public void gotoMainActivity() {
        Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
    }

    @Override
    public void finishCurrentLoginActivity() {
        //关闭当前Activity
        if (getActivity() != null)  getActivity().finish();
    }

    /*
     * 生命周期
     * @return
     */

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     *  假如是因为登陆跳转而跳转到当前(登陆)界面
     *      那么intent里面就有 mCarrier
     *          将其取出
     */
    private LoginCarrier getLoginInterceptorCarrier() {
        //取出登陆拦截信息carrier
        return getActivity().getIntent().getParcelableExtra(LoginInterceptor.INVOKER_CARRIER);
    }

    /**
     * 该方法在 onCreateView后立刻执行
     */
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //carrier可能为null
        LoginCarrier carrier = getLoginInterceptorCarrier();
        mPresenter = new LoginPresenter(this, getActivity(), carrier);
    }

    @Override
    public void onDestroy() {
        //在这里销毁Presenter 防止内存泄漏
        mPresenter.onDestoryPresnter();
        super.onDestroy();
    }

    @Override
    protected View getLoadingTargetView() {
        /*注：必须在外面再嵌套一层*/
        return getCurrentFragmentRootView().findViewById(R.id.id_layout_login);
    }
    @Override
    protected int getFragmentLayoutID() {
        return R.layout.fragment_login;
    }
    @Override
    public void testView() {
        Log.i("试下生命周期", mEditUser.getText().toString().trim());
    }
}
